package com.itheima.controller.store;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.controller.BaseServlet;
import com.itheima.service.impl.store.BrokenServiceImpl;
import com.itheima.service.store.BrokenService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@WebServlet("/store/chart/*")
public class BrokenServlet extends BaseServlet {
    BrokenService brokenService = new BrokenServiceImpl();

    //跳转折线图统计会员数量页面
    private void getMemberReport(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,-12);//获得当前日期之前12个月的日期

        List<String> list = new ArrayList<String>();
        for(int i=0;i<12;i++){
            calendar.add(Calendar.MONTH,1);
            list.add(new SimpleDateFormat("yyyy-MM").format(calendar.getTime()));
        }

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("months",list);

        List<Integer> memberCount = brokenService.findMemberCountByMonth(list);
        map.put("memberCount",memberCount);

        String json=new ObjectMapper().writeValueAsString(map);
        resp.setContentType("text/json;charset=utf-8");
        resp.getWriter().write(json);
//        req.setAttribute("months",list);
//        req.setAttribute("memberCount",memberCount);
//
//        req.getRequestDispatcher("/WEB-INF/pages/store/chart/broken.jsp").forward(req,resp);
    }

    public void getReportMember(HttpServletRequest req, HttpServletResponse resp){
        try {
            req.getRequestDispatcher("/WEB-INF/pages/store/chart/broken.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
